package com.lucifer.cloud.boot.blog.chat.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author lucifer
 * @date 2025/5/12 10:47
 */
public class FileUtil {

    // 指定你要保存的路径
    private static String UPLOAD_DIR = "D:/data/";

    public  static String saveToLocal(MultipartFile file) throws IOException {
        // 确保目标目录存在
        Path uploadPath = Paths.get(UPLOAD_DIR);
        if (!uploadPath.toFile().exists()) {
            uploadPath.toFile().mkdirs();
        }

        // 构建目标文件路径
        File localFile = new File(UPLOAD_DIR + file.getOriginalFilename());

        // 使用 FileOutputStream 写入文件
        try (FileOutputStream fos = new FileOutputStream(localFile)) {
            fos.write(file.getBytes());
        }
        return UPLOAD_DIR + file.getOriginalFilename();
    }
}
